package gen.tools.host;

import gen.tools.Widgets;
import gen.tools.deployByDelivery.SquadHost;
import gen.tools.structures.InfoStruc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryAvailableHost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryAvailableHost() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		PreparedStatement ps2 = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		final ArrayList<SquadHost> shs = new ArrayList<SquadHost>();
		String tmp = "";
		String envId =  request.getParameter("envId");
		
		final String[] distinSqs = request.getParameter("distinSqs").split(wid.splitTokenFOUR); 
		String subsysId = null;
		String strSQL = "SELECT t2.hostname,t1.ip FROM mpsquadhosts t1,hosts t2 WHERE t1.subsysid=? AND t1.envid=? AND t1.squadid=? AND t1.subsysid=t2.subsysid AND t1.ip=t2.ip AND t1.port=t2.port ORDER BY t2.hostname DESC";
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement(strSQL);
			for ( int i=0; i<distinSqs.length; i++ ) {
				final SquadHost sh = new SquadHost();
				if ( i==0) {
					subsysId=distinSqs[i].split(wid.splitTokenONE)[0];
					strSQL = "SELECT t2.webconsoleip  FROM env t1, wcl t2 WHERE t1.webconsolesn=t2.webconsolesn AND t1.subsysid=? AND t1.envid=?";
					ps2 = conn.prepareStatement(strSQL);
					ps2.setString(1, subsysId);
					ps2.setString(2, envId);
					rs2 = ps2.executeQuery();
					if (rs2.next()) { tmp = rs2.getString("webconsoleip"); }		
				}
				sh.setWebConsole( tmp );
				
				ps.setString(1, distinSqs[i].split(wid.splitTokenONE)[0]);
				ps.setString(2, envId);
				ps.setString(3, distinSqs[i].split(wid.splitTokenONE)[1]);
				rs = ps.executeQuery();
				
				final ArrayList<InfoStruc> hts = new ArrayList<InfoStruc>();
				sh.setSubsysid(distinSqs[i].split(wid.splitTokenONE)[0]);
				sh.setSquadid(distinSqs[i].split(wid.splitTokenONE)[1]);
						
				while (rs.next()) {
					final InfoStruc ht = new InfoStruc();
					ht.setId( rs.getString("hostname") );
					ht.setName(rs.getString("ip"));
					hts.add(ht);
				}
				
				sh.setHts(hts);
				shs.add(sh);
			}
		} catch ( Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (rs2 != null) {rs2.close();}
				if (ps!= null) {ps.close();}
				if (ps2!= null) {ps2.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.getWriter().write(new Gson().toJson(shs));
	}

}
